home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / FWResAcc.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  6.4 KB  |  215 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:        FWResAcc.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef   FWRESACC_H
  13. #include "FWResAcc.h"
  14. #endif
  15.  
  16. #ifndef   FWRESFIL_H
  17. #include "FWResFil.h"
  18. #endif
  19.  
  20. #ifndef FWEXCDEF_H
  21. #include "FWExcDef.h"
  22. #endif
  23.  
  24. #ifndef   FWPRIDEB_H
  25. #include "FWPriDeb.h"
  26. #endif
  27.  
  28. #if defined FW_BUILD_MAC && !defined(__RESOURCES__)
  29. #include <Resources.h>
  30. #endif
  31.  
  32. #if defined FW_BUILD_MAC && !defined(__MEMORY__)
  33. #include <Memory.h>
  34. #endif
  35.  
  36. #if FW_LIB_EXPORT_PRAGMAS
  37. #pragma lib_export on
  38. #endif
  39.  
  40. #ifdef FW_BUILD_MAC
  41. #pragma segment FW_ResourceAccess
  42. #endif
  43.  
  44. //========================================================================================
  45. // CLASS FW_CResource
  46. //========================================================================================
  47.  
  48. //----------------------------------------------------------------------------------------
  49. // FW_CResource::FW_CResource
  50. //----------------------------------------------------------------------------------------
  51.  
  52. FW_CResource::FW_CResource(const FW_CResourceFile &file,
  53.                                        FW_ResourceId resourceId,
  54.                                        FW_ResourceType resourceType) :
  55.     fPrivResourceAccess(new FW_CPrivResourceRep(file, resourceId, resourceType))
  56. {
  57.     FW_END_CONSTRUCTOR
  58. }
  59.  
  60. //----------------------------------------------------------------------------------------
  61. // FW_CResource::FW_CResource
  62. //----------------------------------------------------------------------------------------
  63.  
  64. FW_CResource::FW_CResource(const FW_CResource& other) :
  65.     fPrivResourceAccess(other.fPrivResourceAccess)
  66. {
  67.     FW_END_CONSTRUCTOR
  68. }
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // FW_CResource::FW_CResource
  72. //----------------------------------------------------------------------------------------
  73.  
  74. FW_CResource::~FW_CResource()
  75. {
  76.     FW_START_DESTRUCTOR
  77. }
  78.  
  79. //========================================================================================
  80. //    CLASS FW_CAcquireResourceData
  81. //
  82. //    A resource data acquistion helper object.  This object can be used in order
  83. //    to obtain access to resource data in an exception safe manner.
  84. //========================================================================================
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // FW_CAcquireResourceData::FW_CAcquireResourceData
  88. //----------------------------------------------------------------------------------------
  89.  
  90. FW_CAcquireResourceData::FW_CAcquireResourceData(FW_CResource resource) :
  91.     fResource(resource),
  92.     fData(resource.GetData())
  93. {
  94.     FW_END_CONSTRUCTOR
  95. }
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // FW_CAcquireResourceData::~FW_CAcquireResourceData
  99. //----------------------------------------------------------------------------------------
  100.  
  101. FW_CAcquireResourceData::~FW_CAcquireResourceData()
  102. {
  103.     FW_START_DESTRUCTOR
  104.     fResource.ReleaseData();
  105. }
  106.  
  107. //========================================================================================
  108. //    Global Utility Functions
  109. //========================================================================================
  110.  
  111. #ifdef FW_BUILD_WIN
  112. #pragma pack(push,1)
  113. #endif
  114.  
  115. struct FW_SMultiStringResEntry
  116. {
  117.     short fStringId;
  118.     unsigned short fStringOffset;
  119. };
  120.  
  121. struct FW_SMultiStringHeader
  122. {
  123.     unsigned short fStringCount;                // number of entrys
  124.     FW_SMultiStringResEntry fEntryArray[1];        // table of entries
  125.     unsigned short GetStringOffset(short theKey);
  126. };
  127.  
  128. #ifdef FW_BUILD_WIN
  129. #pragma pack(pop)
  130. #endif
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // FW_SMultiStringHeader::GetStringOffset
  134. //----------------------------------------------------------------------------------------
  135.  
  136. static const unsigned short kBadOffset = 0xFFFF;
  137.  
  138. unsigned short FW_SMultiStringHeader::GetStringOffset(short theKey)
  139. {
  140.     short wLo = 0,
  141.     wHi = fStringCount - 1;
  142.     short wValue;                                // trial Value
  143.     // X[wLo..wHi] are ordered, but untested
  144.     do
  145.     {
  146.         short wMid = (wLo + wHi) / 2;
  147.         wValue = fEntryArray[wMid].fStringId;    // try middle Value
  148.         if (wValue <= theKey)
  149.             wLo = wMid + 1;                // X[0..wLo-1] <= theKey
  150.  
  151.         if (wValue >= theKey)
  152.             wHi = wMid - 1;                // X[wHi+1..wMax] >= theKey  X[wLo..wHi] untested
  153.     } while (wLo <= wHi);                // still at least one untested
  154.  
  155.     if (wLo - wHi == 2)
  156.         return fEntryArray[wHi + 1].fStringOffset;
  157.  
  158.     return kBadOffset;
  159. }
  160.  
  161. //========================================================================================
  162. //    Globale Functions
  163. //========================================================================================
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_LoadStringByPosition
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void FW_FUNC_ATTR FW_LoadStringByPosition(FW_CResourceFile &file,
  170.                             FW_ResourceId resourceId,
  171.                             FW_ResourceType resourceType,
  172.                             unsigned short position,
  173.                             FW_CString &string)
  174. {
  175.     FW_CResource resource(file, resourceId, resourceType);
  176.     void *p = resource.GetData();
  177.     
  178.     FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
  179.     FW_ASSERT(head->fStringCount > 0);
  180.     
  181.     // Compute the Offset into the table, based on the position.
  182.     const unsigned short offset = head->fEntryArray[position].fStringOffset;
  183.     
  184.     string.ReplaceAll(((const FW_Char*)p)+offset);
  185.     
  186.     resource.ReleaseData();
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. // FW_LoadStringByID
  191. //----------------------------------------------------------------------------------------
  192.                     
  193. void FW_FUNC_ATTR FW_LoadStringByID(FW_CResourceFile &file,
  194.                             FW_ResourceId resourceId,
  195.                             FW_ResourceType resourceType,
  196.                             unsigned long id,
  197.                             FW_CString &string)
  198. {
  199.     FW_CResource resource(file, resourceId, resourceType);
  200.     void *p = resource.GetData();
  201.     
  202.     FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
  203.     FW_ASSERT(head->fStringCount > 0);
  204.     
  205.     // Compute the Offset into the table, based on the position.
  206.     const unsigned short offset = head->GetStringOffset(id);
  207.     if (offset == kBadOffset)
  208.         FW_ASSERT(FALSE);    // throw an exception, I suppose.
  209.     
  210.     string.ReplaceAll(((const FW_Char*)p)+offset);
  211.     
  212.     resource.ReleaseData();
  213. }
  214.                     
  215.